package com.bawei.rk_01.Login

import androidx.lifecycle.viewModelScope
import com.bawei.base.utils.SingLiveData
import com.bawei.base.utils.bean.CodeEntity
import com.bawei.base.utils.bean.LoginEntity
import com.bawei.base.utils.bean.Res
import com.bawei.base.utils.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class LoginViewModel : BaseViewModel() {

    //返回值--密码登录
    val loginsuccess = SingLiveData<Res<LoginEntity>>()
    val loginfail = SingLiveData<String>()

    /**
     * 用户登录函数
     * 在ViewModel中使用，通过账户和密码进行用户登录
     * @param account 用户账户，用于识别用户
     * @param password 用户密码，用于验证用户身份
     */
    fun login(account:String, password:String){
        viewModelScope.launch {
            try {
                // 创建一个可变映射，用于存储登录所需的账户和密码信息
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["password"] = password

                // 使用Gson将映射转换为JSON字符串，以便发送到服务器
                val s = Gson().toJson(map)

                // 创建请求体，指定内容类型和编码方式，将JSON字符串作为数据包含在内
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(), s)

                // 调用API服务的登录方法，并将响应结果发布到登录成功的LiveData对象中
                loginsuccess.postValue(apiService.login(body))
            }catch (e:Exception){
                // 捕获异常，并将错误信息发布到登录失败的LiveData对象中
                loginfail.postValue(e.message.toString())
            }
        }
    }

    val codesuccess = SingLiveData<Res<CodeEntity>>()
    val codefail = SingLiveData<String>()
    //获取验证码
    fun getCode(phone:String){
        viewModelScope.launch {
            try {
                codesuccess.postValue(apiService.getCode(phone))
            }catch (e:Exception){
                codefail.postValue(e.message.toString())
            }
        }
    }

    val codeligonsuccess = SingLiveData<Res<LoginEntity>>()
    val codeloginfail = SingLiveData<String>()

    fun codelogin(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["captcha"] = captcha
                map["phone"] = phone
                val s = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(), s)
                codeligonsuccess.postValue(apiService.codeLogin(body))
            }catch (e:Exception){
                codeloginfail.postValue(e.message.toString())
            }
        }
    }

    //修改密码
    val xgsuccess = SingLiveData<Res<Boolean>>()
    val xgfail = SingLiveData<String>()

    fun xiugai(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,String>()
                map["account"] = account
                map["captcha"] = captcha
                map["password"] = password
                val s = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(), s)
                xgsuccess.postValue(apiService.xiugai(body))
            }catch (e:Exception){
                xgfail.postValue(e.message.toString())
            }
        }
    }

}